# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=qscintilla
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python2-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-common")
pkgver=2.9
pkgrel=1
pkgdesc="A port to Qt5 of Neil Hodgson's Scintilla C++ editor class"
arch=('any')
license=("GPL")
url="http://www.riverbankcomputing.co.uk/software/qscintilla/intro"
depends=("${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2-pyqt5"
             "${MINGW_PACKAGE_PREFIX}-python3-pyqt5")
options=(!strip staticlibs !buildflags)
source=("https://downloads.sourceforge.net/pyqt/QScintilla-gpl-${pkgver}.tar.gz"
        'configure.py-objdir-support.diff'
        'run-command-via-sh.patch'
        'qmake-install-paths.patch')
sha256sums=('d7c32e32582f93779de861006d87467b38b9ebc06e3d0b32e981cb24369fa417'
            'aaedb6d1784438c42bdf020d3d1db50b94f4862cebab4526c46d453050da871e'
            'fc91ee0acfdae14a31338715f0a59d73dbd41b934fb90b3e4d8c5ba71b05c407'
            'bdf71a27b9b5590dee4cd25450f04a09e1680b3e1787ff7d392b53ba01c4e64f')

prepare() {
  cd QScintilla-gpl-${pkgver}
  patch -p1 -i "${srcdir}"/configure.py-objdir-support.diff
  patch -p1 -i "${srcdir}"/run-command-via-sh.patch
  patch -p1 -i "${srcdir}"/qmake-install-paths.patch

  sed -e "s|%INSTALL_PREFIX%|$(cygpath -wm ${pkgdir})|g" \
    -i Qt4Qt5/qscintilla.pro \
    -i designer-Qt4Qt5/designer.pro
}

build() {
  cd ${srcdir}/QScintilla-gpl-${pkgver}/Qt4Qt5
  ${MINGW_PREFIX}/bin/qmake qscintilla.pro
  make

  cd ../designer-Qt4Qt5
  QMAKEFEATURES=../Qt4Qt5/features ${MINGW_PREFIX}/bin/qmake designer.pro INCLUDEPATH+=../Qt4Qt5 QMAKE_LIBDIR+=../Qt4Qt5/release
  make

  cd ../
  cp -rf Python Python2
}

package_qscintilla() {
  pkgdesc="A port to Qt5 of Neil Hodgson's Scintilla C++ editor class"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd QScintilla-gpl-${pkgver}/Qt4Qt5
  make install

  cd ../designer-Qt4Qt5
  make install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
}

package_python-qscintilla-common() {
  pkgdesc="Common python qscintilla bindings files shared between python3-qscintilla and python2-qscintilla"
  depends=("${MINGW_PACKAGE_PREFIX}-qscintilla")

  cd QScintilla-gpl-${pkgver}/Python
  make DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install

  # Provided by python-qscintilla
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib
}

package_python3-qscintilla() {
  pkgdesc="Python 3.x bindings for QScintilla2"
  depends=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common" "${MINGW_PACKAGE_PREFIX}-python3-pyqt5")

  cd QScintilla-gpl-${pkgver}/Python
  QMAKEFEATURES=../Qt4Qt5/features \
  ${MINGW_PREFIX}/bin/python3 configure.py \
    --pyqt=PyQt5 \
    --qsci-incdir=../Qt4Qt5/ \
    --qsci-libdir=../Qt4Qt5/release/

  make
  make INSTALL_ROOT=${pkgdir} install

  rm -rf "${pkgdir}${MINGW_PREFIX}"/share
}

package_python2-qscintilla() {
  pkgdesc="Python 2.x bindings for QScintilla2"
  depends=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common" "${MINGW_PACKAGE_PREFIX}-python2-pyqt5")

  cd QScintilla-gpl-${pkgver}/Python2
  QMAKEFEATURES=../Qt4Qt5/features \
  ${MINGW_PREFIX}/bin/python2 configure.py \
    --pyqt=PyQt5 \
    --qsci-incdir=../Qt4Qt5/ \
    --qsci-libdir=../Qt4Qt5/release/

  make
  make INSTALL_ROOT=${pkgdir} install

  rm -rf "${pkgdir}${MINGW_PREFIX}"/share
}

### Wrappers

package_mingw-w64-i686-qscintilla() {
  package_qscintilla
}

package_mingw-w64-i686-python-qscintilla-common() {
  package_python-qscintilla-common
}

package_mingw-w64-i686-python2-qscintilla() {
  package_python2-qscintilla
}

package_mingw-w64-i686-python3-qscintilla() {
  package_python3-qscintilla
}

package_mingw-w64-x86_64-qscintilla() {
  package_qscintilla
}

package_mingw-w64-x86_64-python-qscintilla-common() {
  package_python-qscintilla-common
}

package_mingw-w64-x86_64-python2-qscintilla() {
  package_python2-qscintilla
}

package_mingw-w64-x86_64-python3-qscintilla() {
  package_python3-qscintilla
}
